import 'package:flutter/material.dart';
import '../constants/wallet_constants.dart';

/// 网络选择底部弹出框组件
class NetworkSelectorBottomSheet extends StatelessWidget {
  final String currentNetwork;
  final Function(String) onNetworkSelected;

  const NetworkSelectorBottomSheet({
    Key? key,
    required this.currentNetwork,
    required this.onNetworkSelected,
  }) : super(key: key);

  /// 显示网络选择底部弹出框
  static Future<String?> show(
    BuildContext context, {
    required String currentNetwork,
  }) async {
    return await showModalBottomSheet<String>(
      context: context,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      builder: (context) => NetworkSelectorBottomSheet(
        currentNetwork: currentNetwork,
        onNetworkSelected: (networkId) {
          Navigator.pop(context, networkId);
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        color: Color(0xFF1E2139),
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(24),
          topRight: Radius.circular(24),
        ),
      ),
      child: SafeArea(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // 顶部标题栏
            Container(
              padding: const EdgeInsets.all(20),
              child: Row(
                children: [
                  const Icon(
                    Icons.public,
                    color: Color(0xFF6C5CE7),
                    size: 24,
                  ),
                  const SizedBox(width: 12),
                  const Text(
                    '选择网络',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    ),
                  ),
                  const Spacer(),
                  IconButton(
                    onPressed: () => Navigator.pop(context),
                    icon: const Icon(Icons.close, color: Colors.white70),
                  ),
                ],
              ),
            ),
            
            const Divider(color: Colors.white12, height: 1),
            
            // 网络列表
            ListView.builder(
              shrinkWrap: true,
              padding: const EdgeInsets.symmetric(vertical: 8),
              itemCount: WalletConstants.supportedNetworks.length,
              itemBuilder: (context, index) {
                final entry = WalletConstants.supportedNetworks.entries.elementAt(index);
                final networkId = entry.key;
                final network = entry.value;
                final isSelected = networkId == currentNetwork;
                
                return Material(
                  color: Colors.transparent,
                  child: InkWell(
                    onTap: () {
                      onNetworkSelected(networkId);
                    },
                    child: Container(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 20,
                        vertical: 16,
                      ),
                      child: Row(
                        children: [
                          // 网络图标
                          Container(
                            width: 40,
                            height: 40,
                            decoration: BoxDecoration(
                              color: isSelected 
                                ? const Color(0xFF6C5CE7).withOpacity(0.2)
                                : Colors.white.withOpacity(0.05),
                              borderRadius: BorderRadius.circular(12),
                            ),
                            child: Icon(
                              Icons.language,
                              color: isSelected 
                                ? const Color(0xFF6C5CE7)
                                : Colors.white54,
                            ),
                          ),
                          const SizedBox(width: 16),
                          
                          // 网络信息
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  network.name,
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 16,
                                    fontWeight: isSelected 
                                      ? FontWeight.bold 
                                      : FontWeight.normal,
                                  ),
                                ),
                                const SizedBox(height: 4),
                                Text(
                                  network.symbol,
                                  style: const TextStyle(
                                    color: Colors.white54,
                                    fontSize: 14,
                                  ),
                                ),
                              ],
                            ),
                          ),
                          
                          // 选中标记
                          if (isSelected)
                            Container(
                              padding: const EdgeInsets.all(4),
                              decoration: BoxDecoration(
                                color: const Color(0xFF6C5CE7),
                                borderRadius: BorderRadius.circular(12),
                              ),
                              child: const Icon(
                                Icons.check,
                                color: Colors.white,
                                size: 16,
                              ),
                            ),
                        ],
                      ),
                    ),
                  ),
                );
              },
            ),
            
            const SizedBox(height: 8),
          ],
        ),
      ),
    );
  }
}

